package com.atguigu.nio.channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
    public static void main(String[] args) {

        try (FileInputStream fileInputStream = new FileInputStream("1.txt");
             FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
             FileChannel fileInputStreamChannel = fileInputStream.getChannel();
             FileChannel fileOutputStreamChannel = fileOutputStream.getChannel()) {


            ByteBuffer byteBuffer = ByteBuffer.allocate(512);

            while (true) { //循环读取

                //这里有一个重要的操作，一定不要忘了
                /*
                 public final Buffer clear() {
                    position = 0;
                    limit = capacity;
                    mark = -1;
                    return this;
                }
                 */
                byteBuffer.clear(); //清空buffer
                int read = fileInputStreamChannel.read(byteBuffer);
                System.out.println("read =" + read);
                if (read == -1) { //表示读完
                    break;
                }
                //将buffer 中的数据写入到 fileOutputStreamChannel -- 2.txt
                byteBuffer.flip();
                fileOutputStreamChannel.write(byteBuffer);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
